package controller.validator;

import java.util.ArrayList;

import entity.Student;
import exception.InputValidationErrors;

/**
 * Implements validation for adding, updating or deleting student.
 * 
 * @author huangjm.2012, jaslin.wee.2012
 * 
 */
public class StudentValidator extends Validator {

	private String userId;
	private String password;
	private String name;
	private String school;
	private String edollarStr;

	private Student student;

	public Student getStudent() {
		return student;
	}

	public StudentValidator(String userId, String password, String name,
			String school, String edollarStr) {
		super();

		this.userId = userId;
		this.password = password;
		this.name = name;
		this.school = school;
		this.edollarStr = edollarStr;
	}

	@Override
	public void performValidationBeforeAdding() throws InputValidationErrors {

		/*
		 * Array list to store logic error messages
		 */
		ArrayList<String> logicErrors = new ArrayList<String>();

		/*
		 * Validation for duplicate userid
		 */
		try {
			checkForDuplicateStudent(userId);
		} catch (Exception e) {
			logicErrors.add(Validator.DUPLICATE_USERID);
		}

		/*
		 * Validation for invalid edollar
		 */
		double edollar = 0;
		try {
			edollar = validateEdollar(edollarStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_EDOLLAR);
		}

		/*
		 * Throw exception or create entity
		 */
		if (!logicErrors.isEmpty()) {
			throw new InputValidationErrors(logicErrors);
		} else {
			student = new Student(userId, password, name, school, edollar);
		}

	}

	@Override
	public void performValidationBeforeUpdating() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

	@Override
	public void performValidationBeforeDeleting() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

}
